/**
 * Title CustomCookieRememberMeManager
 * Package com.ruoyi.framework.shiro.web.filter
 * Copyright 2024 www.hundsun.com All Rights Reserved.
 *
 * @author gd
 * @date 2024/10/23 11:40 AM
 * @version V5.1.2
 */
package com.ruoyi.framework.shiro.web.filter;

import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * CustomCookieRememberMeManager
 * ClassName CustomCookieRememberMeManager
 * @author gd
 * @date 2024/10/23 11:40 AM
 */
public class CustomCookieRememberMeManager extends CookieRememberMeManager {

    @Override
    public void setCookie(Cookie cookie) {

        // 获取当前请求的HttpServletRequest对象
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attr != null) {
            HttpServletRequest request = attr.getRequest();
            // 获取当前请求的路径
            String requestPath = request.getServletPath();
            // 根据请求路径设置cookie的路径属性
            if (requestPath.startsWith("/app")) {
                // 设置为根路径，使得在排除路径下不使用特定的cookie逻辑
                cookie.setPath("/ruoyi");
            } else {
                // 正常情况下的路径设置
                cookie.setPath("/");
            }
        }
        super.setCookie(cookie);
    }
}